﻿using CodeCreator;
using CodeCreator.CoreObj;
using CodeCreator.Template;
using Common;
using iBing.Common.DbHelper.CoreObj;
using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CodeTemplate.CodeEdit
{
    public partial class FrmCreateTemplate : Form
    {
        public List<TypeMap> typeMapList = new List<TypeMap>();
        private Obj obj = null;
        public FrmCreateTemplate(Obj obj)
        {
            InitializeComponent();
            RefreshTemplates();
            this.obj = obj;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            //模板替换
            if (!Directory.Exists(this.txtCodePath.Text))
            {
                using (FrmMessage fr = new FrmMessage("主人，代码生成目录不存在，请选择！"))
                {
                    fr.ShowDialog(this);
                    this.txtCodePath.Focus();
                }
                return;
            }
            List<string> fileList = new List<string>();
            String templateFilePath, codeFilePath = "";
            //拷贝模板到指定目录下后循环遍历文件
            TemplateInfo template = this.cmbTemplates.SelectedItem as TemplateInfo;
            if (template != null)
            {
                fileList.Clear();
                templateFilePath = template.Path;
                codeFilePath = this.txtCodePath.Text.Trim();

                if (this.chkAddCode.Checked)
                {
                    if (codeFilePath.EndsWith("\\"))
                        codeFilePath += "code";
                    else
                        codeFilePath += "\\code";
                }

                if (!Directory.Exists(codeFilePath))
                {
                    Directory.CreateDirectory(codeFilePath);
                }

                List<string> exceptDir = new List<string>();
                exceptDir.Add(".svn");
                List<string> exceptFile = new List<string>();
                exceptFile.Add("TypeMap.xml");

                fileList = ToolsFile.CopyDirectory(templateFilePath, codeFilePath, template.Path, exceptDir, exceptFile);
            }

            try
            {
                foreach (string file in fileList)
                {
                    FileToReplace.replaceFile(file, obj);
                }

                //删除模板文件
                foreach (string file in fileList)
                {
                    if (File.Exists(file))
                        File.Delete(file);
                }

                using (FrmMessage fr = new FrmMessage("查看", "关闭", "主人，生成完了，是否检查一下？"))
                {
                    if (fr.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                    {
                        System.Diagnostics.Process.Start("Explorer.exe", codeFilePath);
                    }
                }
            }
            catch (Exception ex)
            {
                using (FrmMessage fr = new FrmMessage("查看", "关闭", "出错了，错误：" + ex.Message))
                {
                    fr.ShowDialog(this);
                }
            }
        }



       


        private void RefreshTemplates()
        {
            string strDir = System.Windows.Forms.Application.StartupPath + "/Templates";
            if (Directory.Exists(strDir))
            {
                string[] dirs = Directory.GetDirectories(strDir);
                List<TemplateInfo> templates = new List<TemplateInfo>();
                foreach (string str in dirs)
                {
                    if (!str.EndsWith(".svn"))
                    {
                        DirectoryInfo di = new DirectoryInfo(str);
                        templates.Add(new TemplateInfo(di.Name, di.FullName));
                    }
                }
                this.cmbTemplates.DataSource = templates;
                this.cmbTemplates.DisplayMember = "Name";
            }
        }

        private void cmbTemplates_SelectedIndexChanged(object sender, EventArgs e)
        {
            TemplateInfo template = this.cmbTemplates.SelectedItem as TemplateInfo;
            if (template != null)
            {
                if (File.Exists(template.Path + "\\TypeMap.xml"))
                {
                    this.typeMapList = ToolsSerialize.XmlDeserialize<List<TypeMap>>(template.Path + "\\TypeMap.xml");
                }
                else
                {
                    this.typeMapList.Clear();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnClearDir_Click(object sender, EventArgs e)
        {
            string strPath = this.txtCodePath.Text.Trim();
            if (this.chkAddCode.Checked)
            {
                if (strPath.EndsWith("\\"))
                    strPath += "code\\";
                else
                    strPath += "\\code\\";
            }
            else
            {
                if (!strPath.EndsWith("\\"))
                    strPath += "\\";
            }

            using (FrmMessage fr = new FrmMessage("确定", "取消", "主人，您正在进行极其危险的操作，确定要删除\n" + strPath + "\n目录下所有文件吗？"))
            {
                if (fr.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    if (Directory.Exists(strPath))
                    {
                        foreach (string f in Directory.GetFiles(strPath))
                        {
                            FileSystem.DeleteFile(f, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);

                        }

                        foreach (string d in Directory.GetDirectories(strPath))
                        {
                            FileSystem.DeleteDirectory(d, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
                        }
                    }
                    FrmMessage frOk = new FrmMessage("主人，清理成功,文件已经丢到回收站！");
                    frOk.ShowDialog(this);
                }
            }
        }

        private void btnGetDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog frm = new FolderBrowserDialog();
            if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                this.txtCodePath.Text = frm.SelectedPath;
            }
        }

        private void btnRefreshTemplates_Click(object sender, EventArgs e)
        {
            RefreshTemplates();
        }

        private void btnShowTemplates_Click(object sender, EventArgs e)
        {
            TemplateInfo template = this.cmbTemplates.SelectedItem as TemplateInfo;
            if (template != null)
            {
                if (Directory.Exists(template.Path))
                {
                    System.Diagnostics.Process.Start("Explorer.exe", template.Path);
                }
                else
                {
                    FrmMessage frOk = new FrmMessage("主人，没有模板路径！\n" + template.Path);
                    frOk.ShowDialog(this);
                }
            }
        }

    }
}
